package utils

import (
	"aflow/models"
	"bytes"
	"fmt"
	"strings"
)

func getRepoName(gitRepoUrl string) string {
	parts := strings.Split(gitRepoUrl, "/")
	repoName := parts[len(parts)-1]
	repoName = strings.TrimSuffix(repoName, ".git")
	return repoName
}
func GitCommandBuilder(pipeline models.Pipeline) string {
	// 替换 URL 添加用户名和密码
	gitRepoUrl := pipeline.GitRepoUrl
	gitUser := pipeline.GitUser
	gitPassword := pipeline.GitPassword
	repoPath := pipeline.RepoPath
	branchName := pipeline.BranchName
	repoName := getRepoName(gitRepoUrl)
	authGitUrl := strings.Replace(gitRepoUrl, "://", fmt.Sprintf("://%s:%s@", gitUser, gitPassword), 1)

	var commands bytes.Buffer

	commands.WriteString("set -e\n") // 遇到错误立即退出

	// 创建目录逻辑
	commands.WriteString(fmt.Sprintf("if [ ! -d \"%s\" ]; then\n", repoPath))
	commands.WriteString(fmt.Sprintf("  sudo mkdir -p \"%s\"\n", repoPath))
	commands.WriteString(fmt.Sprintf("  sudo chown -R $(whoami) \"%s\"\n", repoPath))
	commands.WriteString("fi\n\n")

	// cd 到目录
	commands.WriteString(fmt.Sprintf("cd %s\n", repoPath))

	// 判断仓库是否存在
	commands.WriteString(fmt.Sprintf("if [ ! -d %s ]; then\n", repoName))
	commands.WriteString("  echo 'Repository does not exist, cloning...'\n")
	commands.WriteString(fmt.Sprintf("  git clone %s \n", authGitUrl))
	commands.WriteString(fmt.Sprintf("cd %s\n", repoName))
	commands.WriteString(fmt.Sprintf("  git checkout %s\n", branchName))
	commands.WriteString("else\n")
	commands.WriteString("  echo 'Repository exists, checking for updates...'\n")
	commands.WriteString(fmt.Sprintf("cd %s\n", repoName))
	commands.WriteString("  git fetch origin\n")
	commands.WriteString("  LOCAL=$(git rev-parse HEAD)\n")
	commands.WriteString(fmt.Sprintf("  REMOTE=$(git rev-parse origin/%s)\n", branchName))
	commands.WriteString(fmt.Sprintf("  BASE=$(git merge-base HEAD origin/%s)\n", branchName))
	commands.WriteString("  if [ \"$LOCAL\" = \"$REMOTE\" ]; then\n")
	commands.WriteString("    echo 'Already up-to-date'\n")
	commands.WriteString("  else\n")
	commands.WriteString(fmt.Sprintf("    git checkout %s\n", branchName))
	commands.WriteString(fmt.Sprintf("    git pull origin %s\n", branchName))
	commands.WriteString("  fi\n")
	commands.WriteString("fi\n")

	return commands.String()

}

func MavenCommandBuilder(pipeline models.Pipeline) string {

	repoPath := pipeline.RepoPath
	gitRepoUrl := pipeline.GitRepoUrl
	repoName := getRepoName(gitRepoUrl)

	var commands bytes.Buffer

	commands.WriteString("set -e\n") // 遇到错误立即退出

	// cd 到项目目录
	commands.WriteString(fmt.Sprintf("cd %s/%s\n", repoPath, repoName))

	// 执行 Maven 构建命令
	commands.WriteString("source /etc/profile && mvn clean install -DskipTests\n")

	return commands.String()
}

func NpmCommandBuilder(pipeline models.Pipeline) string {

	repoPath := pipeline.RepoPath
	gitRepoUrl := pipeline.GitRepoUrl
	repoName := getRepoName(gitRepoUrl)

	var commands bytes.Buffer

	commands.WriteString("set -e\n") // 遇到错误立即退出

	// cd 到项目目录
	commands.WriteString(fmt.Sprintf("cd %s/%s\n", repoPath, repoName))

	// 执行 Maven 构建命令
	commands.WriteString("source /etc/profile && npm install && npm run build\n")

	return commands.String()
}

func GolangCommandBuilder(pipeline models.Pipeline) string {

	repoPath := pipeline.RepoPath
	gitRepoUrl := pipeline.GitRepoUrl
	repoName := getRepoName(gitRepoUrl)

	var commands bytes.Buffer

	commands.WriteString("set -e\n") // 遇到错误立即退出

	// cd 到项目目录
	commands.WriteString(fmt.Sprintf("cd %s/%s\n", repoPath, repoName))

	// 执行 Maven 构建命令
	commands.WriteString("source /etc/profile && go build\n")

	return commands.String()
}

func DockerCommandBuilder(pipeline models.Pipeline) string {

	dockerImageName := pipeline.DockerImageName
	dockerImageTag := pipeline.DockerImageTag
	dockerRunCommand := pipeline.DockerRunCommand
	repoPath := pipeline.RepoPath

	dockerfilePath := pipeline.DockerfilePath
	gitRepoUrl := pipeline.GitRepoUrl
	repoName := getRepoName(gitRepoUrl)

	dockerImage := fmt.Sprintf("%s:%s", dockerImageName, dockerImageTag)

	var commands bytes.Buffer

	// cd 到项目目录
	commands.WriteString(fmt.Sprintf("cd %s/%s\n", repoPath, repoName))

	// 如果 mvnExecPath 存在，则进入该目录
	if dockerfilePath != "" {
		commands.WriteString(fmt.Sprintf("cd %s\n", dockerfilePath))
	}

	// Docker 操作命令
	commands.WriteString(fmt.Sprintf("docker stop %s\n", dockerImageName))
	commands.WriteString(fmt.Sprintf("docker rm %s\n", dockerImageName))
	commands.WriteString(fmt.Sprintf("docker rmi %s\n", dockerImage))
	commands.WriteString(fmt.Sprintf("docker build -t %s .\n", dockerImage))
	commands.WriteString(fmt.Sprintf("%s\n", dockerRunCommand))

	return commands.String()
}
